//
//  MainViewController.swift
//  SK-微博
//
//  Created by 姜胜凯 on 15/10/8.
//  Copyright © 2015年 姜胜凯. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    addChildViewControllers()
    }
    
    override func viewDidAppear(animated: Bool) {
        setUcomposeButton()
    }
    
    
    //给button按钮添加事件
    func clickComposeButton(){
    print(__FUNCTION__)
    
    }
    //设置button的frame
    func setUcomposeButton(){
    let w  = tabBar.bounds.width/CGFloat(viewControllers!.count)
    let rect = CGRectMake(0 , 0, w, tabBar.bounds.height)
     composeButton.frame = CGRectOffset(rect, 2*w, 0)
    }
    
    //添加所有控制器
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
        
        
    }
    //添加子控制器
    private func addChildViewController(vc : UITableViewController,title : String,imageName : String) {
        tabBar.tintColor = UIColor.orangeColor()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
   
//懒加载button
    lazy private var composeButton : UIButton = {
    
    let button = UIButton()
    button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
    button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
    button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    self.tabBar.addSubview(button)
     button.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
    return button
    }()
    
    
    
}
